home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 December / maximum-cd-2009-12.iso / DiscContents / gimp-2.7.0-i686-setup.exe / {app} / share / gimp / 2.0 / scripts / perspective-shadow.scm < prev    next >
Encoding:
GIMP Script-Fu Script  |  2009-08-19  |  8.1 KB  |  215 lines

  1. ; GIMP - The GNU Image Manipulation Program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ;
  4. ; This program is free software: you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation; either version 3 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ;
  17. ;
  18. ; perspective-shadow.scm   version 1.2   2000/11/08
  19. ;
  20. ; Copyright (C) 1997-2000 Sven Neumann <sven@gimp.org>
  21. ;
  22. ;
  23. ; Adds a perspective shadow of the current selection or alpha-channel
  24. ; as a layer below the active layer
  25. ;
  26.  
  27. (define (script-fu-perspective-shadow image
  28.                                       drawable
  29.                                       alpha
  30.                                       rel-distance
  31.                                       rel-length
  32.                                       shadow-blur
  33.                                       shadow-color
  34.                                       shadow-opacity
  35.                                       interpolation
  36.                                       allow-resize)
  37.   (let* (
  38.         (shadow-blur (max shadow-blur 0))
  39.         (shadow-opacity (min shadow-opacity 100))
  40.         (shadow-opacity (max shadow-opacity 0))
  41.         (rel-length (abs rel-length))
  42.         (alpha (* (/ alpha 180) *pi*))
  43.         (type (car (gimp-drawable-type-with-alpha drawable)))
  44.         (image-width (car (gimp-image-width image)))
  45.         (image-height (car (gimp-image-height image)))
  46.         (from-selection 0)
  47.         (active-selection 0)
  48.         (shadow-layer 0)
  49.         )
  50.  
  51.   (gimp-context-push)
  52.  
  53.   (if (> rel-distance 24) (set! rel-distance 999999))
  54.   (if (= rel-distance rel-length) (set! rel-distance (+ rel-distance 0.01)))
  55.  
  56.   (gimp-image-undo-group-start image)
  57.  
  58.   (gimp-layer-add-alpha drawable)
  59.   (if (= (car (gimp-selection-is-empty image)) TRUE)
  60.       (begin
  61.         (gimp-selection-layer-alpha drawable)
  62.         (set! from-selection FALSE))
  63.       (begin
  64.         (set! from-selection TRUE)
  65.         (set! active-selection (car (gimp-selection-save image)))))
  66.  
  67.   (let* ((selection-bounds (gimp-selection-bounds image))
  68.          (select-offset-x (cadr selection-bounds))
  69.          (select-offset-y (caddr selection-bounds))
  70.          (select-width (- (cadr (cddr selection-bounds)) select-offset-x))
  71.          (select-height (- (caddr (cddr selection-bounds)) select-offset-y))
  72.  
  73.          (abs-length (* rel-length select-height))
  74.          (abs-distance (* rel-distance select-height))
  75.          (half-bottom-width (/ select-width 2))
  76.          (half-top-width (* half-bottom-width
  77.                           (/ (- rel-distance rel-length) rel-distance)))
  78.  
  79.          (x0 (+ select-offset-x (+ (- half-bottom-width half-top-width)
  80.                                  (* (cos alpha) abs-length))))
  81.          (y0 (+ select-offset-y (- select-height
  82.                                  (* (sin alpha) abs-length))))
  83.          (x1 (+ x0 (* 2 half-top-width)))
  84.          (y1 y0)
  85.          (x2 select-offset-x)
  86.          (y2 (+ select-offset-y select-height))
  87.          (x3 (+ x2 select-width))
  88.          (y3 y2)
  89.  
  90.          (shadow-width (+ (- (max x1 x3) (min x0 x2)) (* 2 shadow-blur)))
  91.          (shadow-height (+ (- (max y1 y3) (min y0 y2)) (* 2 shadow-blur)))
  92.          (shadow-offset-x (- (min x0 x2) shadow-blur))
  93.          (shadow-offset-y (- (min y0 y2) shadow-blur)))
  94.  
  95.  
  96.     (set! shadow-layer (car (gimp-layer-new image
  97.                                             select-width
  98.                                             select-height
  99.                                             type
  100.                                             "Perspective Shadow"
  101.                                             shadow-opacity
  102.                                             NORMAL-MODE)))
  103.  
  104.  
  105.     (gimp-image-add-layer image shadow-layer -1)
  106.     (gimp-layer-set-offsets shadow-layer select-offset-x select-offset-y)
  107.     (gimp-drawable-fill shadow-layer TRANSPARENT-FILL)
  108.     (gimp-context-set-background shadow-color)
  109.     (gimp-edit-fill shadow-layer BACKGROUND-FILL)
  110.     (gimp-selection-none image)
  111.  
  112.     (if (= allow-resize TRUE)
  113.         (let* ((new-image-width image-width)
  114.                (new-image-height image-height)
  115.                (image-offset-x 0)
  116.                (image-offset-y 0))
  117.  
  118.           (if (< shadow-offset-x 0)
  119.               (begin
  120.                 (set! image-offset-x (abs shadow-offset-x))
  121.                 (set! new-image-width (+ new-image-width image-offset-x))
  122.                 ; adjust to new coordinate system
  123.                 (set! x0 (+ x0 image-offset-x))
  124.                 (set! x1 (+ x1 image-offset-x))
  125.                 (set! x2 (+ x2 image-offset-x))
  126.                 (set! x3 (+ x3 image-offset-x))
  127.               ))
  128.  
  129.           (if (< shadow-offset-y 0)
  130.               (begin
  131.                 (set! image-offset-y (abs shadow-offset-y))
  132.                 (set! new-image-height (+ new-image-height image-offset-y))
  133.                 ; adjust to new coordinate system
  134.                 (set! y0 (+ y0 image-offset-y))
  135.                 (set! y1 (+ y1 image-offset-y))
  136.                 (set! y2 (+ y2 image-offset-y))
  137.                 (set! y3 (+ y3 image-offset-y))
  138.               ))
  139.  
  140.           (if (> (+ shadow-width shadow-offset-x) new-image-width)
  141.               (set! new-image-width (+ shadow-width shadow-offset-x)))
  142.  
  143.           (if (> (+ shadow-height shadow-offset-y) new-image-height)
  144.               (set! new-image-height (+ shadow-height shadow-offset-y)))
  145.           (gimp-image-resize image
  146.                              new-image-width
  147.                              new-image-height
  148.                              image-offset-x
  149.                              image-offset-y)))
  150.  
  151.     (gimp-drawable-transform-perspective shadow-layer
  152.                       x0 y0
  153.                       x1 y1
  154.                       x2 y2
  155.                       x3 y3
  156.                       TRANSFORM-FORWARD
  157.                       interpolation
  158.                       TRUE 3 TRANSFORM-RESIZE-ADJUST)
  159.  
  160.     (if (>= shadow-blur 1.0)
  161.         (begin
  162.           (gimp-layer-set-lock-alpha shadow-layer FALSE)
  163.           (gimp-layer-resize shadow-layer
  164.                              shadow-width
  165.                              shadow-height
  166.                              shadow-blur
  167.                              shadow-blur)
  168.           (plug-in-gauss-rle RUN-NONINTERACTIVE
  169.                              image
  170.                              shadow-layer
  171.                              shadow-blur
  172.                              TRUE
  173.                              TRUE))))
  174.  
  175.   (if (= from-selection TRUE)
  176.       (begin
  177.         (gimp-selection-load active-selection)
  178.         (gimp-edit-clear shadow-layer)
  179.         (gimp-image-remove-channel image active-selection)))
  180.  
  181.   (if (and
  182.        (= (car (gimp-layer-is-floating-sel drawable)) 0)
  183.        (= from-selection FALSE))
  184.       (gimp-image-raise-layer image drawable))
  185.  
  186.   (gimp-image-set-active-layer image drawable)
  187.   (gimp-image-undo-group-end image)
  188.   (gimp-displays-flush)
  189.  
  190.   (gimp-context-pop)
  191.   )
  192. )
  193.  
  194. (script-fu-register "script-fu-perspective-shadow"
  195.   _"_Perspective..."
  196.   _"Add a perspective shadow to the selected region (or alpha)"
  197.   "Sven Neumann <sven@gimp.org>"
  198.   "Sven Neumann"
  199.   "2000/11/08"
  200.   "RGB* GRAY*"
  201.   SF-IMAGE       "Image"                        0
  202.   SF-DRAWABLE    "Drawable"                     0
  203.   SF-ADJUSTMENT _"Angle"                        '(45 0 180 1 10 1 0)
  204.   SF-ADJUSTMENT _"Relative distance of horizon" '(5 0.1 24.1 0.1 1 1 1)
  205.   SF-ADJUSTMENT _"Relative length of shadow"    '(1 0.1 24   0.1 1 1 1)
  206.   SF-ADJUSTMENT _"Blur radius"                  '(3 0 1024 1 10 0 0)
  207.   SF-COLOR      _"Color"                        '(0 0 0)
  208.   SF-ADJUSTMENT _"Opacity"                      '(80 0 100 1 10 0 0)
  209.   SF-ENUM       _"Interpolation"                '("InterpolationType" "linear")
  210.   SF-TOGGLE     _"Allow resizing"               FALSE
  211. )
  212.  
  213. (script-fu-menu-register "script-fu-perspective-shadow"
  214.                          "<Image>/Filters/Light and Shadow/Shadow")
  215.